//
//  HYQHUD.m
//  HYQHUD_Example
//
//  Created by zhuxingjian on 2018/8/27.
//  Copyright © 2018年 zxj1989. All rights reserved.
//

#import "HYQHUD.h"

@interface HYQLoadingView : UIView

@property (nonatomic, strong) UIView *loading;

@end

@implementation HYQLoadingView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        _loading = [[UIView alloc] initWithFrame:CGRectMake((CGRectGetWidth(frame) - 26) / 2, (CGRectGetHeight(frame) - 26) / 2, 26, 26)];
        _loading.backgroundColor = [UIColor whiteColor];
        _loading.layer.cornerRadius = 13;
        _loading.layer.borderWidth = 5;
        _loading.layer.borderColor = [UIColor colorWithRed:247 / 255.0 green:78 / 255.0 blue:70 / 255.0 alpha:1].CGColor;
        [self addSubview:self.loading];
        [self addAnimations];
    }
    return self;
}

- (void)addAnimations
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.duration = 0.8;
    animation.fromValue = @(1);
    animation.toValue = @(1.4);
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    animation.repeatCount = MAXFLOAT;
    animation.autoreverses = YES;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    
    CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation1.duration = 0.8;
    animation1.fromValue = @(1);
    animation1.toValue = @(0.6);
    animation1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    animation1.repeatCount = MAXFLOAT;
    animation1.autoreverses = YES;
    animation1.removedOnCompletion = NO;
    animation1.fillMode = kCAFillModeForwards;
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[animation, animation1];
    group.duration = 1.0;
    group.repeatCount = MAXFLOAT;
    group.autoreverses = YES;
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    [self.loading.layer addAnimation:group forKey:@"groupAnimation"];
    
}

- (CGSize)intrinsicContentSize
{
    return self.frame.size;
}

@end

@interface HYQHUD ()

@property (nonatomic, strong) HYQMBProgressHUD *globalHUD;
@property (nonatomic, assign) NSTimeInterval toastTimeOut;

@end

@implementation HYQHUD

static HYQHUD *_hud;
+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _hud = [[HYQHUD alloc] init];
    });
    return _hud;
}

+ (void)setToastTimeOut:(NSTimeInterval)timeOut;
{
    [HYQHUD sharedInstance].toastTimeOut = timeOut;
}

+ (void)setHUDDefaultStyle:(HYQMBProgressHUD *)hud enableTouch:(BOOL)enableTouch
{
    hud.mode = HYQMBProgressHUDModeIndeterminate;
    hud.contentColor = [UIColor whiteColor];
    hud.minSize = CGSizeMake(85, 85);
    hud.label.font = [UIFont systemFontOfSize:15];
    hud.bezelView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
    hud.backgroundView.userInteractionEnabled = !enableTouch;
    hud.userInteractionEnabled = !enableTouch;
}

+ (HYQMBProgressHUD *)showLoadingInView:(UIView *)superView;
{
    HYQMBProgressHUD *hud = [[HYQMBProgressHUD alloc] initWithView:superView];
    hud.mode = HYQMBProgressHUDModeCustomView;
    hud.backgroundColor = [UIColor whiteColor];
    [superView addSubview:hud];
    [hud showAnimated:YES];
    
    HYQLoadingView *loadingView = [[HYQLoadingView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    hud.backgroundView.style = HYQMBProgressHUDBackgroundStyleSolidColor;
    hud.backgroundView.color = [UIColor clearColor];
    hud.bezelView.style = HYQMBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.color = [UIColor clearColor];
    hud.customView = loadingView;
    return hud;
}

+ (void)showHUDInView:(UIView *)superView;
{
    if ([HYQMBProgressHUD HUDForView:superView]) {
        return;
    }
    HYQMBProgressHUD *hud = [HYQMBProgressHUD showHUDAddedTo:superView animated:YES];
    [self setHUDDefaultStyle:hud enableTouch:NO];
}

+ (void)showHUDInView:(UIView *)superView enableTouch:(BOOL)enableTouch;
{
    if ([HYQMBProgressHUD HUDForView:superView]) {
        return;
    }
    HYQMBProgressHUD *hud = [HYQMBProgressHUD showHUDAddedTo:superView animated:YES];
    [self setHUDDefaultStyle:hud enableTouch:enableTouch];
}

+ (void)hidHUDFromView:(UIView *)superView;
{
    [HYQMBProgressHUD hideHUDForView:superView animated:YES];
}

+ (void)showGlobalHUD;
{
    if ([HYQHUD sharedInstance].globalHUD) {
        return;
    }
    UIWindow *window = [self lastWindow];
    [HYQHUD sharedInstance].globalHUD = [HYQMBProgressHUD showHUDAddedTo:window animated:YES];
    [self setHUDDefaultStyle:[HYQHUD sharedInstance].globalHUD enableTouch:NO];
}

+ (void)hidGlobalHUD;
{
    [HYQHUD sharedInstance].globalHUD.completionBlock = ^{
        [HYQHUD sharedInstance].globalHUD = nil;
    };
    [[HYQHUD sharedInstance].globalHUD hideAnimated:YES];
}

+ (HYQMBProgressHUD *)showHUDInView:(UIView *)superView
                        progress:(NSProgress *)progressObject
                           title:(NSString *)title;
{
    HYQMBProgressHUD *preHUD = [HYQMBProgressHUD HUDForView:superView];
    if (preHUD) {
        return preHUD;
    }
    HYQMBProgressHUD *hud = [HYQMBProgressHUD showHUDAddedTo:superView animated:YES];
    [self setHUDDefaultStyle:hud enableTouch:NO];
    hud.mode = HYQMBProgressHUDModeDeterminate;
    hud.label.text = title;
    hud.progressObject = progressObject;
    return hud;
}

+ (void)showToast:(NSString *)toast;
{
    if (!toast.length) {
        return;
    }
    UIWindow *window = [self lastWindow];
    BOOL keyboardShow = [self keyboardShow];
    HYQMBProgressHUD *hud = [[HYQMBProgressHUD alloc] initWithView:window];
    [self setHUDDefaultStyle:hud enableTouch:YES];
    hud.mode = HYQMBProgressHUDModeText;
    hud.minSize = CGSizeZero;
    hud.margin = 12;
    hud.detailsLabel.text = toast;
    hud.detailsLabel.font = [UIFont systemFontOfSize:15];
    hud.animationType = HYQMBProgressHUDAnimationZoomOut;
    if (!keyboardShow) {
        hud.offset = CGPointMake(0, window.bounds.size.height / 2 - 150);
    }
    [window addSubview:hud];
    [hud showAnimated:YES];
    [hud hideAnimated:YES afterDelay:[HYQHUD sharedInstance].toastTimeOut ? [HYQHUD sharedInstance].toastTimeOut : 2];
}

+ (UIWindow *)lastWindow
{
    NSArray *windows = [UIApplication sharedApplication].windows;
    for(UIWindow *window in [windows reverseObjectEnumerator]) {
        if ([NSStringFromClass([window class]) isEqualToString:@"UIWindow"] &&
            CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds))
            return window;
    }
    return [UIApplication sharedApplication].keyWindow;
}

+ (BOOL)keyboardShow
{
    NSArray *windows = [UIApplication sharedApplication].windows;
    for(UIWindow *window in [windows reverseObjectEnumerator]) {
        if ([NSStringFromClass([window class]) isEqualToString:@"UIRemoteKeyboardWindow"] &&
            CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds))
            return YES;
    }
    return NO;
}


@end
